import os, fnmatch

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET


#{urn:jboss:domain:1.5}extensions
#{urn:jboss:domain:1.5}system-properties
#{urn:jboss:domain:1.5}management
#{urn:jboss:domain:1.5}profiles
#{urn:jboss:domain:1.5}interfaces
#{urn:jboss:domain:1.5}socket-binding-groups
#{urn:jboss:domain:1.5}server-groups


#CFD=('/apps/barc-middleware/applications/jboss-as/servers/')
CFD = ('/home/ben/Jboss/jboss-eap-6.2/domain/configuration')
ConfFiles = []
for root, dirname, filenames in os.walk(CFD):
    for filename in fnmatch.filter(filenames, 'domain.xml'):
        ConfFiles.append(os.path.join(root, filename))

for conf in ConfFiles:
    print conf
    xmldoc = ET.ElementTree(file=''+conf)
    #print type(xmldoc)
    confroot = xmldoc.getroot()
    #print type(confroot)
    instname = conf.split('/')[1]
    print 'Configuration Report for Instance: '+instname
    
    #quick dump of the data
    #for elem in xmldoc.findall('.//*'):
    #    print elem.tag, elem.attrib
    
    #Get the JVM Names and Heaps
    for a in list(confroot.iter('{urn:jboss:domain:1.5}server-group')):
      svrgrp = str(a.attrib).split(':')[2].split('}')[0]
      for b in a:
	jvmname = str(b.attrib).split(':')[1].split('}')[0]
	for c in b:
	  memtype = str(c.tag).split('}')[1]
	  memvalue = str(c.attrib).split('{')[1].split('}')[0]
	  print svrgrp, jvmname, memtype, memvalue

    print "Socket Binding Groups"
    for a in list(confroot.iter('{urn:jboss:domain:1.5}socket-binding-groups')):
      #print a.tag, a.attrib
      for b in a:
	inf = str(b.attrib).split(',')[0].split(':')[1]
	infname = str(b.attrib).split(',')[1].split(':')[1].split('}')[0]
	#print b.tag,b.attrib
	for c in b:
	  infor = str(c.attrib).split('{')[1]
	  inform = infor[:-1]
	  print inf, infname, inform
	  
    print "Configured Interfaces"
    for a in list(confroot.iter('{urn:jboss:domain:1.5}interfaces')):
      for b in a:
	intface = str(b.attrib).split(':')[1].split('}')[0]
	print intface
	   
      print "Active Extensions"
      for a in list(confroot.iter('{urn:jboss:domain:1.5}extensions')):
	#print a.tag, a.attrib
	for b in a:
	  extensions = str(b.attrib).split(':')[1].split('}')[0]
	  print extensions

      print "Management Information"
      for a in list(confroot.iter('{urn:jboss:domain:1.5}management')):
	for b in a:
	  for c in b:
	    for d in c:
	      print "Configured Roles:", str(d.attrib).split(':')[1].split('}')[0]
	      for e in d:
		for f in e:
		  print "User Database:", str(f.attrib).split(':')[1].split('}')[0]
